package jdbc;

import java.sql.*;

/**
 * @author oldliu
 * @since 1.0
 */
public class TestSelect {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url = "jdbc:mysql://localhost:3306/java75?useSSL=false&useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true&allowMultiQueries=true";
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            con = DriverManager.getConnection(url, "root", "root1234");
            System.out.println(con.getClass().getName());
            st = con.createStatement();
            System.out.println(st.getClass().getName());
            rs = st.executeQuery("select *  from s_subject order by id desc");
            while (rs.next()) {
                long id = rs.getLong("id");
                String sname = rs.getString("subjectname");
                int classhour = rs.getInt("classhour");
                long grdid = rs.getLong("gradeid");
                System.out.printf("ID=%d,sname=%s,Xueshi %d,grade =%d\n", id, sname, classhour, grdid);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {

                if (st != null)
                    st.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
            try {

                if (con != null)
                    con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
